package com.superc.shared.ui;

import java.util.Date;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.datepicker.client.DateBox;

public class DateTimeBoxEx extends DateBox {
	
	private Date oldDate=null;
	
	public Date getOldDate(){
		return oldDate;
	}
	
	public DateTimeBoxEx(){
		super.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss")));
		super.addValueChangeHandler(new ValueChangeHandler<Date>() {
			public void onValueChange(ValueChangeEvent<Date> event) {
				if(oldDate==null) {
					oldDate=event.getValue();
					final DateTimeBoxEx dateTimeBoxEx=(DateTimeBoxEx)event.getSource();
					dateTimeBoxEx.getDatePicker().addValueChangeHandler(new ValueChangeHandler<Date>() {
						@SuppressWarnings("deprecation")
						public void onValueChange(ValueChangeEvent<Date> event) {
							oldDate.setYear(event.getValue().getYear());
							oldDate.setMonth(event.getValue().getMonth());
							oldDate.setDate(event.getValue().getDate());
							dateTimeBoxEx.setValue(oldDate);
							Window.alert(oldDate.toLocaleString());
						}
					});
				}
			}
		});
	}
	
}
